<template>
  <a @click="dialog = true">
    <span>设置</span>
    <a-modal v-model="dialog" title="设置日期" @ok="submit">
      <a-form-model
        ref="form"
        :rules="rules"
        :model="form"
        :label-col="labelCol"
        :wrapper-col="wrapperCol"
        :hideRequiredMark="true"
      >
        <a-form-model-item label="加贷出借人手续费：">
          <a-input addon-after="%" v-model="rate" disabled />
        </a-form-model-item>
        <a-form-model-item label="加贷出借人手续费付款方式：">
          <a-select default-value="立即资本化" v-model="feePayType" disabled>
            <a-select-option :value="1">
              立即付
            </a-select-option>
            <a-select-option :value="4">
              立即资本化
            </a-select-option>
          </a-select>
        </a-form-model-item>
        <a-form-model-item label="生效日期：" prop="workDate">
          <a-date-picker v-model="form.workDate" :disabled-date="disabledDate" :disabled="Archive == 1" />
        </a-form-model-item>
      </a-form-model>
    </a-modal>
  </a>
</template>
<script>
import { updataAddLoan, getList, getDetailList } from '@/api/common'
import moment from 'moment'
import { commonRule, dateRule } from '@/utils/rules'
export default {
  name: 'LoanDate',
  props: ['record'],
  data() {
    return {
      labelCol: { span: 9 },
      wrapperCol: { span: 12 },
      dialog: false,
      Archive: 0,
      rate: '',
      feePayType: '',
      start: null,
      form: {
        workDate: null
      },
      rules: {
        workDate: dateRule
      }
    }
  },
  watch: {
    dialog(v) {
      if (v) {
        this.getData()
      }
    }
  },
  methods: {
    disabledDate(endValue) {
      const startValue = this.start
      if (!endValue || !startValue) {
        return false
      }
      return startValue.valueOf() >= endValue.valueOf()
    },
    async getData() {
      const id = this.$route.query.id
      const res = await getDetailList(id)
      this.Archive = res.result.data.Archive
      this.rate = this.record.rate
      this.feePayType = this.record.feePayType
      if (this.record.workDate) {
        this.form.workDate = moment(this.record.workDate * 1000)
      }
      const form = {
        page: 1,
        limit: 1,
        projectIds: this.$route.query.id
      }
      const resTime = await getList(form)
      const Time = resTime.result.data.list[0]
      this.start = Time.firstLoanTime ? moment(Time.firstLoanTime * 1000) : null
      // console.log(this.start)
    },
    submit() {
      const form = Object.assign({}, this.form)
      form.workDate = Number(form.workDate.startOf('day').format('X'))
      this.$refs.form.validate(valid => {
        if (valid) {
          updataAddLoan(this.record.id, form)
        }
      })
      this.dialog = false
    }
  }
}
</script>
<style lang="scss" scoped></style>
